home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-09-28 | 6.3 KB | 204 lines | [TEXT/CWIE] |
- /*
- File: Station.java
-
- Copyright: © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
-
- Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
- ("Apple") in consideration of your agreement to the following terms, and your
- use, installation, modification or redistribution of this Apple software
- constitutes acceptance of these terms. If you do not agree with these terms,
- please do not use, install, modify or redistribute this Apple software.
-
- In consideration of your agreement to abide by the following terms, and subject
- to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
- copyrights in this original Apple software (the "Apple Software"), to use,
- reproduce, modify and redistribute the Apple Software, with or without
- modifications, in source and/or binary forms; provided that if you redistribute
- the Apple Software in its entirety and without modifications, you must retain
- this notice and the following text and disclaimers in all such redistributions of
- the Apple Software. Neither the name, trademarks, service marks or logos of
- Apple Computer, Inc. may be used to endorse or promote products derived from the
- Apple Software without specific prior written permission from Apple. Except as
- expressly stated in this notice, no other rights or licenses, express or implied,
- are granted by Apple herein, including but not limited to any patent rights that
- may be infringed by your derivative works or by other works in which the Apple
- Software may be incorporated.
-
- The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
- WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
- WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
- COMBINATION WITH YOUR PRODUCTS.
-
- IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
- OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
- (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- Change History (most recent first):
-
- */
-
-
- package com.apple.jens.radio;
-
- import java.io.*;
- import java.net.*;
- import java.util.Vector;
-
-
- /** The Station creates a DJ thread,
- then listens on a particular port and accepts incoming connections,
- dispatching each one to its own Transmitter thread.
- */
- public class Station extends Thread {
-
- public static final String kPropPort = "port",
- kPropName = "name",
- kPropGenre = "genre",
- kPropDJ = "dj", // DJ class
- kPropHost = "host", // (human) host of this station
- kPropHostURL = "host-url",
- kPropHomePage = "home-page";
-
- public static final int kDefaultPort = 8000;
-
-
- /** Creates a Station and starts its thread waiting for incoming connections.
- @param props The Properties read from the station's configuration file. */
- public Station( RadioProperties props ) throws IOException {
- super("Station");
- fProps = props;
-
- fPort = props.getIntProperty(kPropPort,kDefaultPort);
- setName("Station "+getStationName());
-
- fDJ = (DJ) props.getObjectProperty(kPropDJ);
- if( fDJ == null )
- fDJ = new FileDJ();
- fDJ.setStation(this);
-
- start();
- }
-
-
- public void run( ) {
- fDJ.start();
-
- ServerSocket socket = null;
- try{
- socket = new ServerSocket(fPort);
-
- LOG(1,"*** Up and running! Address is <"+getAddress().getHostName()+":"+getPort()+">");
- while(true) {
- Socket inSocket = socket.accept(); // Blocks until client connects
- LOG(1,"Station received an incoming connection from "
- +inSocket.getInetAddress().getHostName()
- +" -- now "+(getTransmitterCount()+1)+" listeners");
-
- Transmitter trans = new Transmitter(fDJ,inSocket);
- fTransmitters.addElement(trans);
- fMaxTransmitters = Math.max(fMaxTransmitters, fTransmitters.size());
- trans.start();
- }
- }catch( IOException x ) {
- System.err.println("Station died with exception:");
- x.printStackTrace(System.err);
- } finally {
- if( socket != null ) {
- try{
- socket.close();
- }catch( IOException x ) {
- }
- }
- }
- }
-
-
- void transmitterStopped( Transmitter trans ) {
- fTransmitters.removeElement(trans);
- LOG(1,"Down to "+getTransmitterCount()+" listeners, from max of "+getMaxTransmitterCount());
- }
-
-
- // ACCESSORS:
-
-
- /** Returns the Station's Properties object. */
- public RadioProperties getProperties( ) {
- return fProps;
- }
-
-
- /** Returns the name of the channel being served by this Station.
- (The method getName is from Thread and returns the name of the thread.) */
- public String getStationName( ) {
- return fProps.getProperty(kPropName);
- }
-
-
- /** Returns the genre of the music being served by this Station. */
- public String getGenre( ) {
- return fProps.getProperty(kPropGenre);
- }
-
-
- /** Returns the address of this Station. */
- public InetAddress getAddress( ) {
- try{
- return InetAddress.getLocalHost();
- }catch( UnknownHostException x ) {
- return null;
- }
- }
-
-
- /** Returns the port number this Station is listening on. */
- public int getPort( ) {
- return fPort;
- }
-
-
- /** Returns the current number of active Transmitters. */
- public int getTransmitterCount( ) {
- return fTransmitters.size();
- }
-
-
- /** Returns the maximum number of active Transmitters
- during the lifespan of this Station. */
- public int getMaxTransmitterCount( ) {
- return fMaxTransmitters;
- }
-
-
- /** Returns an array of all active Transmitters of this Station. */
- public Transmitter[] getTransmitters( ) {
- synchronized(fTransmitters) {
- int n = fTransmitters.size();
- Transmitter[] trans = new Transmitter[n];
- fTransmitters.copyInto(trans);
- return trans;
- }
- }
-
-
- private void LOG( int level, String msg ) {
- Radio.LOG(level,getName(),msg);
- }
-
-
- // INSTANCE DATA:
-
-
- private RadioProperties fProps;
- private DJ fDJ;
- private int fPort;
- private Vector fTransmitters = new Vector();
- private int fMaxTransmitters;
-
- }
-